Using a UserControl in Asp.Net

In the last chapter we have created User Control, and now we will try to use it for the first time. Choose a page in your project, or just create a new one for the purpose, and open it, the first thing that is the first thing we declare is our usertransestrol. It can be done on either page where it is used, or in the web.config file globally. There is no performance difference, but when declaring UserControls in the web.config file, using Controls has to be in a separate directory than page (S)

For now, let's declare it within the page. Add the following line below the standard page declaration:


<%@ Register TagPrefix="My" TagName="UserInfoBoxControl" Src="~/UserInfoBoxControl.ascx" %>

Make sure that the src value matches the path of your UserControl file. Now you can use user control in your page, just like any other control. For example, in this way:


<My:UserInfoBoxControl runat="server" ID="MyUserInfoBoxControl" />

If you look at the page now, you will see our user control in action, though the information will be a little bit ... limited, we have to set a price for the defined assets, because things get a little more interesting fortunately, It is very easy:


<My:UserInfoBoxControl runat="server" ID="MyUserInfoBoxControl" UserName="John Doe" UserAge="45" UserCountry="Australia" />

You see, every public or protected member can be lowered, when we use our control, we can easily get access to them. However, with this unique user control, it is likely that you will get information from an external resource, such as a database, and then popping up the user control. This usually covers the codebind of the page, so how can we do it? Very handy, actually in the CodeBehind of the page, try something like this:


protected void Page_Load(object sender, EventArgs e)
{
    // These values can come from anywhere, but right now, we just hardcode them
    MyUserInfoBoxControl.UserName = "Jane Doe";
    MyUserInfoBoxControl.UserAge = 33;
    MyUserInfoBoxControl.UserCountry = "Germany";
}

Loading dynamically

Sometimes you may want to declare user control on your page rather than dynamically adding it. It is actually very simple too. You need an existing control where you can add user control, for example a panel if there is no logical control on your page, you can create one for this purpose - placeholder Control is for such situations.

On your page, define it like this:


<asp:PlaceHolder runat="server" ID="phUserInfoBox" />

In the CodeBehind of the page, we add the control like this:


phUserInfoBox.Controls.Add(LoadControl("~/UserInfoBoxControl.ascx"));

We use the LoadControl method to instantiate UserControl by specifying the path. It's very easy, but it's also pretty anonymous - because we just use the load control method, we can not actually use our own custom property. To do this, we need to know about it. On the page, add the following announcement on top:


<%@ Reference Control="~/UserInfoBoxControl.ascx" %>

Now we can use the UserInfoBoxControl class as if it was a regular class, it also means that we can change this type of user tran by the LoadControl method. In the next example, we do this, we set the attributes, and finally, we add it to the placeholder:


UserInfoBoxControl userInfoBoxControl = (UserInfoBoxControl)LoadControl("~/UserInfoBoxControl.ascx");
userInfoBoxControl.UserName = "John Doe";
userInfoBoxControl.UserAge = 78;
userInfoBoxControl.UserCountry = "Spain";
phUserInfoBox.Controls.Add(userInfoBoxControl);

This will come in handy, for example, when you are adding multiple instances of the same user control on the same page, because you can do it within a loop